package util.biz.yzwl.model;

import util.biz.yzwl.URLCon;
import util.model.common.FieldInfo;
import util.model.common.TableInfo;
import util.util.DevFileUtil;
import util.util.DevModelUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 创建实体
 * @author: liW
 * @date: 2020/9/3 9:53
 */
public class CreateModel {

    private static final String MODLOU_CREATE_PATH = URLCon.MODLOU_CREATE_PATH;
    public static String MODLOU_OUT_PATH = URLCon.MODLOU_OUT_PATH;

    public static String classPath = "D:\\lw\\10project\\work2\\yuzhen_dlp_be\\dlp\\dlp-infraservice\\dlp-infraservice-datacenter\\src\\main\\java\\com\\mgmiot\\dlp\\infraservice\\datacenter\\model\\entity";
    public static boolean classPathOut = true;

    private  static int SHEET_PAGE = 4;
    private final static int START_ROW = 1;

    public static void main(String[] args) {
        int start = 9;
        int length = 1;

        for (int i = start; i < start + length; i++) {
            SHEET_PAGE = i;
            createModel();
            createDao();
            createVo();
        }
    }


    private static void createModel() {
        // 获取字段信息
        TableInfo tableInfo = DevModelUtil.getTableInfo(MODLOU_CREATE_PATH, SHEET_PAGE, START_ROW);
        String tableDBName = tableInfo.getTableDBName();
        String tableDBTitle = tableInfo.getTableTitle();

        List<FieldInfo> mysqlFields = tableInfo.getFieldList();

        StringBuilder sb = new StringBuilder();
        for (FieldInfo mysqlField : mysqlFields) {
            sb.append(DevModelUtil.createMysqlModel(mysqlField));
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String date = sdf.format(new Date());

        String value = "import com.baomidou.mybatisplus.annotation.TableField;\n" +
            "import com.baomidou.mybatisplus.annotation.TableName;\n" +
            "import com.mgmiot.dlp.component.mysql.model.BaseBean;\n" +
            "import lombok.Data;\n" +
            "import lombok.EqualsAndHashCode;\n" +
            "\n" +
            "import java.time.LocalDateTime;\n" +
            "\n" +
            "/**\n" +
            " * %s\n" +
            " *\n" +
            " * @author: liW\n" +
            " * @date: %s\n" +
            " */\n" +
            "@EqualsAndHashCode(callSuper = true)\n" +
            "@Data\n" +
            "@TableName(\"%s\")\n" +
            "public class %s extends BaseBean {"
            + sb.toString()
            + "}";

        String tableName = DevModelUtil.dBName2BeanName(tableDBName);



        value = String.format(value, tableDBTitle, date, tableDBName, tableName);
        String outPath = MODLOU_OUT_PATH + "entity\\" + tableName + ".java";
        DevFileUtil.createFile(value, outPath);
        if(classPathOut) {
            String packageStr = classPath.split("src\\\\main\\\\java\\\\")[1].replace("\\", ".");
            value = "package " + packageStr + ";\n\n" + value;
            String outClass = classPath + "\\" + tableName + ".java";
            DevFileUtil.createFile(value, outClass);
        }
    }

    private static void createDao() {
        // 获取字段信息
        TableInfo tableInfo = DevModelUtil.getTableInfo(MODLOU_CREATE_PATH, SHEET_PAGE, START_ROW);
        String tableDBName = tableInfo.getTableDBName();
        String tableDBTitle = tableInfo.getTableTitle();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String date = sdf.format(new Date());

        String value = "import com.baomidou.mybatisplus.core.mapper.BaseMapper;\n" +
                "import org.springframework.stereotype.Repository;\n" +
                "\n" +
                "/**\n" +
                " * %s\n" +
                " *\n" +
                " * @author: liW\n" +
                " * @date: %s\n" +
                " */\n" +
                "@Repository\n" +
                "public interface %s extends BaseMapper<%s> {\n" +
                "\n" +
                "}";

        String tableName = DevModelUtil.dBName2BeanName(tableDBName);
        String daoName = DevModelUtil.dBName2DaoName(tableDBName);
        value = String.format(value, tableDBTitle, date, daoName, tableName);
        String outPath = MODLOU_OUT_PATH +  "dao\\" + daoName + ".java";
        DevFileUtil.createFile(value, outPath);
//        System.out.println(value);
    }

    private static void createXml() {
        // 获取字段信息
        TableInfo tableInfo = DevModelUtil.getTableInfo(MODLOU_CREATE_PATH, SHEET_PAGE, START_ROW);
        String tableDBName = tableInfo.getTableDBName();
        String tableDBTitle = tableInfo.getTableTitle();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String date = sdf.format(new Date());

        String value = "import com.baomidou.mybatisplus.core.mapper.BaseMapper;\n" +
            "import com.mgmiot.dlp.ems.model.entity.%s;\n" +
            "import org.springframework.stereotype.Repository;\n" +
            "\n" +
            "/**\n" +
            " * %s\n" +
            " *\n" +
            " * @author: liW\n" +
            " * @date: %s\n" +
            " */\n" +
            "@Repository\n" +
            "public interface %s extends BaseMapper<%s> {\n" +
            "\n" +
            "}";

        String tableName = DevModelUtil.dBName2BeanName(tableDBName);
        String daoName = DevModelUtil.dBName2DaoName(tableDBName);
        value = String.format(value, tableName, tableDBTitle, date, daoName, tableName);
        String outPath = MODLOU_OUT_PATH + "xml\\" + daoName + ".java";
        DevFileUtil.createFile(value, outPath);
//        System.out.println(value);
    }


    private static void createVo() {
        // 获取字段信息
        TableInfo tableInfo = DevModelUtil.getTableInfo(MODLOU_CREATE_PATH, SHEET_PAGE, START_ROW);
        String tableDBName = tableInfo.getTableDBName();
        String tableDBTitle = tableInfo.getTableTitle();

        List<FieldInfo> mysqlFields = tableInfo.getFieldList();

        StringBuilder sb = new StringBuilder();
        for (FieldInfo mysqlField : mysqlFields) {
            sb.append(DevModelUtil.createMysqlVo(mysqlField));
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String date = sdf.format(new Date());

        String value =
                "import lombok.Data;\n" +
                "import io.swagger.annotations.ApiModelProperty;\n" +
                "\n" +
                "\n" +
                "/**\n" +
                " * %s\n" +
                " *\n" +
                " * @author: liW\n" +
                " * @date: %s\n" +
                " */\n" +
                "@Data\n" +
                "public class %s  {"
                + sb.toString()
                + "}";

        String tableVOName = DevModelUtil.dBName2ResVoName(tableDBName);
        value = String.format(value, tableDBTitle, date, tableVOName);
        String outPath = MODLOU_OUT_PATH  + "vo\\" + tableVOName + ".java";
        DevFileUtil.createFile(value, outPath);
//        System.out.println(value);
    }


}
